# -*- coding: utf-8 -*-


from selenium import webdriver
from selenium.webdriver.common.by import By
import time

"""
抓包: 模拟登录依赖的是cookie
selenium模拟登陆: 登录成功，可以拿到登陆之后的cookie
发请求，借助登录之后的cookie，就可以拿到登录之后的数据
"""

driver = webdriver.Chrome()
# 最大化
driver.maximize_window()
# 加载网页
driver.get('https://i.qq.com/')
time.sleep(2)
# 切换iframe标签
driver.switch_to.frame('login_frame')

# 元素定位
img_tag = driver.find_element(By.ID, 'img_out_1836189218')
img_tag.click()

# 跳到了登录的界面了。 立马获取cookie了嘛
time.sleep(2)

# 获取cookie
cookies = driver.get_cookies()
# print(cookies)

li = []
for cookie in cookies:
    # print(cookie)
    # 只需要字典当中的name值和value值  name=value; name1=value1;
    name = cookie['name']
    value = cookie['value']
    # print(name + '=' + value)
    li.append(name + '=' + value)
cookie = '; '.join(li)
print(cookie)

import requests

url = 'https://user.qzone.qq.com/1836189218'


# 反爬参数不一定只会有一个
head = {
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
    'cookie':cookie
}
# 发请求， 获取响应
res = requests.get(url, headers=head)

# 打印相应内容
print(res.text)